package com.yimi.yinhepda.views.offline.load;

import com.yimi.yinhepda.MainActivity;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.CompanyTableDao;
import com.yimi.yinhepda.db.dao.UnloadScanTableDao;
import com.yimi.yinhepda.db.dao.UnloadTaskTableDao;
import com.yimi.yinhepda.db.table.CompanyTable;
import com.pda.ym.db.table.UnloadScanTable;
import com.yimi.yinhepda.db.table.UnloadTaskTable;
import com.yimi.yinhepda.entity.bean.OfflineTaskEntity;
import com.yimi.yinhepda.entity.bean.OfflineUploadChildEntity;
import com.yimi.yinhepda.entity.bean.OfflineUploadEntity;
import com.yimi.yinhepda.entity.bean.OfflineUploadSealNumEntity;
import com.yimi.yinhepda.entity.bean.PdaReceiveVo;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.DateHelper;
import com.pda.uce.commom.util.LogUtil;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.views.offline.OffLineOperatorListActivity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lj
 */
public class OffLineLoadUtil {

    /**
     * 获取离线装车数据
     */
    public static List<OfflineTaskEntity> offlineLoadData(String userCode, String deptCode) {
        UnloadTaskTableDao dao = new UnloadTaskTableDao();
        List<OfflineTaskEntity> listTaskEntity = new ArrayList<>();
        //查询暂存的配置单号
        List<UnloadTaskTable> listUnloadTaskTable = dao.findByOperTypeCode1(userCode, deptCode);

        if (listUnloadTaskTable != null && !listUnloadTaskTable.isEmpty()) {
            for (UnloadTaskTable item : listUnloadTaskTable) {
                if (item.getOfflineTaskNo() != null && item.getOfflineTaskNo().startsWith("Z")) {
                    //删除大于三天的离线数据
                    if (item.getIsUpload() == 1) {
                        Date threeDaybefore = DateHelper.getOldDate(-3);
                        Date createTime = DateHelper.StrToDate(item.getCreateTime(), DateHelper.simpleTimeFormat);
                        if (threeDaybefore.getTime() > createTime.getTime()) {
                            try {
                                dao.delete(item);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    //获取离线装车数据
                    OfflineTaskEntity entity = new OfflineTaskEntity();
                    entity.setPriceNum(item.getPriceNum());
                    entity.setDestDeptName(item.getDestDeptName());
                    entity.setOfflineTaskNo(item.getOfflineTaskNo());
                    entity.setCarNo(item.getCarNo());
                    entity.setLoadType(item.getLoadType());
                    entity.setIsUpload(item.getIsUpload());
                    listTaskEntity.add(entity);
                }
            }
        }
        return listTaskEntity;
    }

    /**
     * 离线装车提交的数据
     */
    public static PdaReceiveVo submitData(List<OfflineTaskEntity> listTaskEntity, String userCode, int operTypeCode) {
        SharedPreferUtil sharedPreferUtil = SharedPreferUtil.getInstance();
        PdaReceiveVo pdaReceiveVo = new PdaReceiveVo();
        pdaReceiveVo.setCreatername(sharedPreferUtil.getEmpName());
        List<OfflineUploadEntity> taskLists = new ArrayList<>();
        List<OfflineUploadChildEntity> childLists = new ArrayList<>();
        List<OfflineUploadSealNumEntity> sealNumLists = new ArrayList<>();
        for (OfflineTaskEntity entity : listTaskEntity) {
            if (entity.getIsUpload() == 1) {
                //已上传忽略
                continue;
            }
            //已发车数据
            if (entity.getLoadType() == 1) {
                //离线主单任务
                UnloadTaskTable taskTable = new UnloadTaskTableDao().findData(entity.getOfflineTaskNo(), userCode);
                OfflineUploadEntity upLoadEntity = new OfflineUploadEntity();
                upLoadEntity.setOfflineTaskNo(taskTable.getOfflineTaskNo());
                upLoadEntity.setStowageNo(taskTable.getStowageNo());
                upLoadEntity.setTaskNo(taskTable.getTask());
                if (taskTable.getIsCompleteCar() == 1) {
                    //已是勾选整车
                    upLoadEntity.setBizTypeCode(3);
                } else {
                    upLoadEntity.setBizTypeCode(taskTable.getBizTypeCode());
                }
                upLoadEntity.setOfflineType(1);
                upLoadEntity.setSrcDeptCode(taskTable.getSrcDeptCode());
                upLoadEntity.setSrcDeptName(taskTable.getSrcDeptName());
                upLoadEntity.setDestDeptCode(taskTable.getDestDeptCode());
                upLoadEntity.setDestDeptName(taskTable.getDestDeptName());
                upLoadEntity.setToCompCode(taskTable.getToCompCode());
                CompanyTable companyTable = new CompanyTableDao().findCompCodeDate(taskTable.getToCompCode());
                if (companyTable != null) {
                    upLoadEntity.setToCompName(companyTable.getShortName());
                }
                upLoadEntity.setCarNo(taskTable.getCarNo());
                String createTime = taskTable.getCreateTime();
                upLoadEntity.setOperTimes(createTime);
                upLoadEntity.setCompCode(sharedPreferUtil.getCompanyCode());
                upLoadEntity.setCompName(sharedPreferUtil.getCompanyName());
                upLoadEntity.setPriceNum(taskTable.getPriceNum());
                upLoadEntity.setPiecesNum(taskTable.getPiecesNum());
                upLoadEntity.setCreater(userCode);
                upLoadEntity.setCreaterName(sharedPreferUtil.getEmpName());
                upLoadEntity.setCreaterTimes(createTime);

                //离线子单数据
                List<UnloadScanTable> unloadDatas = new UnloadScanTableDao()
                        .getDataByStowageAndType(entity.getOfflineTaskNo(), operTypeCode, userCode);
                for (UnloadScanTable scanTable : unloadDatas) {
                    //设置主单月台号
                    upLoadEntity.setPlantformNo(scanTable.getPlatformNo());

                    String scanTime = scanTable.getScanTime();
                    OfflineUploadChildEntity childEntity = new OfflineUploadChildEntity();
                    childEntity.setOfflineTaskNo(scanTable.getOfflineTaskNo());
                    childEntity.setWaybillNo(scanTable.getWaybillNo());
                    childEntity.setChildWaybillNo(scanTable.getChildWaybillNo());
                    childEntity.setDeptCode(sharedPreferUtil.getDeptCode());
                    childEntity.setOperTypeCode(1);
                    childEntity.setOperEmpCode(userCode);
                    childEntity.setOperEmp(sharedPreferUtil.getEmpName());
                    childEntity.setOperTimes(scanTime);
                    childEntity.setInputType(scanTable.getInputType());
                    childEntity.setOperPersonCodes(scanTable.getOperPersonCodes());
                    childEntity.setOperPersonNames(scanTable.getOperPersonNames());
                    childEntity.setPlantformNo(scanTable.getPlatformNo());
                    childEntity.setRemark(scanTable.getRemark());
                    childEntity.setCompName(sharedPreferUtil.getCompanyName());
                    childEntity.setCreater(userCode);
                    childEntity.setCreaterTimes(scanTime);
                    childEntity.setModifier(userCode);
                    childEntity.setModifierTimes(scanTime);
                    childLists.add(childEntity);
                }

                taskLists.add(upLoadEntity);

                //离线封签数据
                String sealSignTime = taskTable.getSealSignTime();
                OfflineUploadSealNumEntity sealNumEntity = new OfflineUploadSealNumEntity();
                sealNumEntity.setOfflineTaskNo(taskTable.getOfflineTaskNo());
                sealNumEntity.setStowageNo(taskTable.getStowageNo());
                sealNumEntity.setDeptCode(sharedPreferUtil.getDeptCode());
                sealNumEntity.setDeptName(sharedPreferUtil.getValue(Common.DEPT_NAME));
                sealNumEntity.setSealEmpNo(userCode);
                sealNumEntity.setSealEmpName(sharedPreferUtil.getEmpName());
                sealNumEntity.setSealTimes(sealSignTime);
                sealNumEntity.setSealType(1);
                if (taskTable.getSealSignNo() != null) {
                    String[] sealNums = taskTable.getSealSignNo().split(",");
                    for (String sealNum : sealNums) {
                        if (sealNumEntity.getSealNumber1() == null) {
                            sealNumEntity.setSealNumber1(sealNum);
                        } else if (sealNumEntity.getSealNumber2() == null) {
                            sealNumEntity.setSealNumber2(sealNum);
                        } else if (sealNumEntity.getSealNumber3() == null) {
                            sealNumEntity.setSealNumber3(sealNum);
                        } else if (sealNumEntity.getSealNumber4() == null) {
                            sealNumEntity.setSealNumber4(sealNum);
                        } else if (sealNumEntity.getSealNumber5() == null) {
                            sealNumEntity.setSealNumber5(sealNum);
                        } else if (sealNumEntity.getSealNumber6() == null) {
                            sealNumEntity.setSealNumber6(sealNum);
                        }
                    }
                }
                sealNumEntity.setCompCode(sharedPreferUtil.getCompanyCode());
                sealNumEntity.setCompName(sharedPreferUtil.getCompanyName());
                sealNumEntity.setCreater(userCode);
                sealNumEntity.setCreaterTimes(sealSignTime);
                sealNumEntity.setModifier(userCode);
                sealNumEntity.setModifierTimes(sealSignTime);
                sealNumLists.add(sealNumEntity);
            }
        }
        pdaReceiveVo.setTaskEntityList(taskLists);
        pdaReceiveVo.setScanEntityList(childLists);
        pdaReceiveVo.setSealEntityList(sealNumLists);
        return pdaReceiveVo;
    }

    public static void requestNet(final PdaReceiveVo pdaReceiveVo, final String userCode, final int operTypeCode,
                                  final UICallBack callback) {
        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setResponsClazz(Object.class)
                .setUrl(Constants.URL_OFFLINE_UPLOAD_INFO)
                .setRequestObject(pdaReceiveVo)
                .setConnTimeOut(5 * 1000)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        callback.onError(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        UnloadTaskTableDao mUnloadTaskTableDao = new UnloadTaskTableDao();
                        UnloadScanTableDao mUnloadScanTableDao = new UnloadScanTableDao();
                        List<OfflineUploadEntity> uploadEntities = pdaReceiveVo.getTaskEntityList();
                        for (OfflineUploadEntity entity : uploadEntities) {
                            try {
                                List<UnloadTaskTable> list = mUnloadTaskTableDao.findStowage(entity.getOfflineTaskNo(), userCode);
                                for (UnloadTaskTable taskTable : list) {
                                    taskTable.setIsUpload(1);
                                    mUnloadTaskTableDao.update(taskTable);
                                }
                                List<UnloadScanTable> unloadScan = mUnloadScanTableDao.getDataByStowageAndType(entity.getOfflineTaskNo(), operTypeCode, userCode);
                                for (UnloadScanTable scanTable : unloadScan) {
                                    scanTable.setIsUpload(1);
                                    mUnloadScanTableDao.update(scanTable);
                                }
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        callback.onSuccess(obj);
                    }
                });
    }

    /**
     * 上传离线装车数据
     */
    public static void uploadOfflineLoadData() {
        SharedPreferUtil sharedPreferUtil = SharedPreferUtil.getInstance();
        //3-装车
        int operTypeCode = 3;
        String userCode = sharedPreferUtil.getValue(Common.USER_CODE);
        //获取离线装车数据
        List<OfflineTaskEntity> listTaskEntity = offlineLoadData(userCode, sharedPreferUtil.getDeptCode());
        if (listTaskEntity == null || listTaskEntity.isEmpty()) {
            return;
        }
        //获取组装的提交数据
        PdaReceiveVo pdaReceiveVo = submitData(listTaskEntity, userCode, operTypeCode);
        requestNet(pdaReceiveVo, userCode, operTypeCode, new UICallBack() {
            @Override
            public void onError(String msg) {
            }

            @Override
            public void onSuccess(Object obj) {
                if (OffLineLoadCacheActivity.handler != null) {
                    OffLineLoadCacheActivity.handler.sendEmptyMessage(0);
                }
                if (MainActivity.handler != null) {
                    MainActivity.handler.sendEmptyMessage(MainActivity.UPDATE_OFFLINE_DATA_NUM);
                }
                if (OffLineOperatorListActivity.handler != null) {
                    OffLineOperatorListActivity.handler.sendEmptyMessage(0);
                }
                LogUtil.e("自动上传离线装车数据成功！");
            }
        });
    }
}
